<?php
if(!defined('INCLUDE')) exit();

// Require needed file
require_once CORE_DIRECTORY.OS_PATH_SEPARATOR.'baseUpload.php';

$pluginList = array();
// Function which load needed file to allow plugin instanciation
// Populate the $pluginList array
function loadPlugin_upload($e)
{
	global $pluginList;
	if(is_dir($e) && file_exists($e.OS_PATH_SEPARATOR.'index.php'))
	{
		debug($e.OS_PATH_SEPARATOR.'index.php', 'File "%v" loaded', JGLIB_DEBUG_INFO);
		include_once $e.OS_PATH_SEPARATOR.'index.php';
		$pluginList[] = 'uploadPlugins_'.$pluginName;
	}
}

// Scan plugin directory and load them
$scan = new Scandirr(UPLOAD_PLUGINS_DIRECTORY, 'loadPlugin_upload', 1);

if(empty($pluginList))
{
	throw new Exception('No uploadPlugins found');
}

$action = false;
// Handled request (GET or POST) for a specific plugin
if(isset($_REQUEST['plugin']))
{
	$plugin = $_REQUEST['plugin'];
	$action = true;
}

// Action involving a specific plugin
if(isset($plugin)) 
{
	// Is this plugin exit ?
	if(!in_array('uploadPlugins_'.$plugin, $pluginList))
	{
		throw new Exception('This plugin doesn\'t exist (is it installed ?)');
	}
	
	$pluginName = 'uploadPlugins_'.$plugin;
	$upload = new $pluginName();
	// Do the processing if a request exists see ahead
	if($action)
	{
		$files = debug($upload->process(),'Uploaded file list', JGLIB_DEBUG_PER3);
	}
	// Show the XHTML form if applicable
	else
	{
		return $upload->XHTMLForm();
	}
}
// Get the whole plugin list for the view
else 
{
	foreach($pluginList as $plugin)
	{
		debug(str_replace('uploadPlugins_', '', $plugin), '<a href="add.php?show=%v">%v</a><br />', JGLIB_DEBUG_INFO);
	}
}
